package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.tanhua.commons.template.HuanXinTemplate;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.AnnouncementVO;
import com.tanhua.domain.vo.ContactVO;
import com.tanhua.domain.vo.MessageCommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.AnnouncementApi;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author anshuai
 * @create 2021-02-04 18:12
 */
@Service
public class MessageService {

    @Reference
    private FriendApi friendApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private CommentApi commentApi;
    @Reference
    private AnnouncementApi announcementApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;

    /**
     * 添加好友
     * @param targetUserId 目标用户Id
     * @return
     */
    public ResponseEntity addContact(Long targetUserId) {
        //1.把好友关系保存到MongoDB
        friendApi.addFriend(UserHolder.getUserId(),targetUserId);

        //2.把好友关系注册到环信
        huanXinTemplate.addContactUser(UserHolder.getUserId(),targetUserId);

        return ResponseEntity.ok(null);
    }

    /**
     * 查询好友列表
     * @param page 当前页
     * @param pagesize 每页几条
     * @param keyword 搜索关键字
     */
    public ResponseEntity findContactList(int page, int pagesize, String keyword) {
        //1.查询好友列表
        PageResult<Friend> pageResult = friendApi.findFriendList(page, pagesize, keyword, UserHolder.getUserId());

        //转换vo
        List<Friend> friendList = pageResult.getItems();
        List<ContactVO> voList = new ArrayList<>();
        for (Friend friend : friendList) {
            ContactVO vo = new ContactVO();

            vo.setUserId(friend.getFriendId().toString());

            UserInfo userInfo = userInfoApi.findById(friend.getFriendId());
            BeanUtils.copyProperties(userInfo,vo);

            String city = StringUtils.substringBefore(userInfo.getCity(), "-");
            vo.setCity(city);

            voList.add(vo);
        }

        //3.构造返回值
        PageResult<ContactVO> result = new PageResult<>();
        BeanUtils.copyProperties(pageResult,result);
        result.setItems(voList);

        return ResponseEntity.ok(result);
    }

    /**
     *  消息-点赞列表（commentType 1）
     */
    public ResponseEntity findCommentList(int page, int pagesize, int commentType) {
        //1. 调用API，从MongoDB里分页查询针对当前用户的操作列表
        PageResult<Comment> pageResult = commentApi.findCommentsByUser(UserHolder.getUserId(), commentType, page, pagesize);

        //2.转换vo
        List<Comment> commentList = pageResult.getItems();
        List<MessageCommentVO> voList = new ArrayList<>();
        for (Comment comment : commentList) {
            MessageCommentVO vo = new MessageCommentVO();

            //查询评论人的信息
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(userInfo,vo);
            vo.setId(comment.getId().toHexString());

            String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date(comment.getCreated()));
            vo.setCreateDate(dateStr);

            voList.add(vo);
        }

        PageResult<MessageCommentVO> result = new PageResult<>();
        BeanUtils.copyProperties(pageResult,result);
        result.setItems(voList);

        return ResponseEntity.ok(result);
    }

    /**
     * 消息-公告列表
     * @return
     */
    public ResponseEntity findAnnouncementsList(int page,int pagesize) {
        //1.调用API,从mysql分页查询公告列表
        IPage<Announcement> iPage = announcementApi.findAnnouncementList(page,pagesize);

        //2.转换vo
        //获取公告集合
        List<Announcement> announcementList = iPage.getRecords();
        List<AnnouncementVO> voList = new ArrayList<>();
        for (Announcement announcement : announcementList) {
            AnnouncementVO vo = new AnnouncementVO();
            String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(announcement.getCreated());
            vo.setCreateDate(dateStr);
            vo.setDescription(announcement.getDescription());
            vo.setId(announcement.getId().toString());
            vo.setTitle(announcement.getTitle());

            voList.add(vo);
        }

        //构造返回值
        PageResult<AnnouncementVO> result = new PageResult<>();
        result.setPage(page);
        result.setPages((int)iPage.getPages());
        result.setPagesize(pagesize);
        result.setCounts((int) iPage.getTotal());
        result.setItems(voList);

        return ResponseEntity.ok(result);
    }
}
